package com.ruoyi.zsyh.controller;

import java.io.File;
import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.zsyh.domain.ZsyhPartsQrcode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhPartsQrcodeInstall;
import com.ruoyi.zsyh.service.IZsyhPartsQrcodeInstallService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品安装信息Controller
 * 
 * @author yuexl
 * @date 2022-01-13
 */
@Controller
@RequestMapping("/zsyh/partsQrcodeInstall")
public class ZsyhPartsQrcodeInstallController extends BaseController
{
    private String prefix = "zsyh/partsQrcodeInstall";

    @Autowired
    private IZsyhPartsQrcodeInstallService zsyhPartsQrcodeInstallService;

    @RequiresPermissions("zsyh:partsQrcodeInstall:view")
    @GetMapping()
    public String partsQrcodeInstall()
    {
        return prefix + "/partsQrcodeInstall";
    }

    /**
     * 查询产品安装信息列表
     */
    @RequiresPermissions("zsyh:partsQrcodeInstall:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhPartsQrcodeInstall zsyhPartsQrcodeInstall)
    {
        startPage();
        List<ZsyhPartsQrcodeInstall> list = zsyhPartsQrcodeInstallService.selectZsyhPartsQrcodeInstallList(zsyhPartsQrcodeInstall);
        return getDataTable(list);
    }

    /**
     * 导出产品安装信息列表
     */
    @RequiresPermissions("zsyh:partsQrcodeInstall:export")
    @Log(title = "产品安装信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhPartsQrcodeInstall zsyhPartsQrcodeInstall)
    {
        List<ZsyhPartsQrcodeInstall> list = zsyhPartsQrcodeInstallService.selectZsyhPartsQrcodeInstallList(zsyhPartsQrcodeInstall);
        ExcelUtil<ZsyhPartsQrcodeInstall> util = new ExcelUtil<ZsyhPartsQrcodeInstall>(ZsyhPartsQrcodeInstall.class);
        return util.exportExcel(list, "产品安装信息数据");
    }

    /**
     * 新增产品安装信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品安装信息
     */
    @RequiresPermissions("zsyh:partsQrcodeInstall:add")
    @Log(title = "产品安装信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhPartsQrcodeInstall zsyhPartsQrcodeInstall)
    {
        return toAjax(zsyhPartsQrcodeInstallService.insertZsyhPartsQrcodeInstall(zsyhPartsQrcodeInstall));
    }

    /**
     * 修改产品安装信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPartsQrcodeInstall zsyhPartsQrcodeInstall = zsyhPartsQrcodeInstallService.selectZsyhPartsQrcodeInstallById(id);
        mmap.put("zsyhPartsQrcodeInstall", zsyhPartsQrcodeInstall);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品安装信息
     */
    @RequiresPermissions("zsyh:partsQrcodeInstall:edit")
    @Log(title = "产品安装信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhPartsQrcodeInstall zsyhPartsQrcodeInstall)
    {
        return toAjax(zsyhPartsQrcodeInstallService.updateZsyhPartsQrcodeInstall(zsyhPartsQrcodeInstall));
    }

    /**
     * 删除产品安装信息
     */
    @RequiresPermissions("zsyh:partsQrcodeInstall:remove")
    @Log(title = "产品安装信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] installIds = Convert.toStrArray(ids);
        int num =0;
        for(String installId : installIds){
            ZsyhPartsQrcodeInstall zsyhPartsQrcodeInstall = zsyhPartsQrcodeInstallService.selectZsyhPartsQrcodeInstallById(installId);
            num += zsyhPartsQrcodeInstallService.deleteZsyhPartsQrcodeInstallByIds(installId);
            if(num >0){
                //删除数据的同时要删除相关图片
                if(EmptyUtils.isNotEmpty(zsyhPartsQrcodeInstall.getInstallImages())){
                    String[] filePath = zsyhPartsQrcodeInstall.getInstallImages().split("\\|");
                    for(String tempPath :filePath){
                        tempPath = tempPath.replace("https://zsyhnew.zzzsyh.com/profile","/home/zsyh/uploadPath");
                        System.out.println("path-------"+tempPath);
                        File file = new File(tempPath);
                        file.delete();
                    }
                }
            }
        }
        return toAjax(num);
    }
}
